package com.magnificent.atlantis.client.data.task.planner;

import com.magnificent.atlantis.client.data.task.TaskHelper;
import com.magnificent.atlantis.client.data.task.creator.AbstractCreatorTask;
import com.magnificent.atlantis.client.data.task.creator.CreatorBasicTask;
import com.magnificent.atlantis.client.data.task.planner.additional.Comment;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * User: Babanin
 * Date: 04.03.2009
 * Time: 0:53:41
 */
@XmlRootElement(name = "task")
public class BasicPlannerTask extends AbstractPlannerTask {
    protected int progress;
    private List<Comment> comments;

    private List<AbstractPlannerTask> subPlannerTasks;

    public BasicPlannerTask() {
        comments = new ArrayList<Comment>();
        subPlannerTasks = new ArrayList<AbstractPlannerTask>();
    }

    public BasicPlannerTask(AbstractCreatorTask abstractCreatorTask) {
        super(abstractCreatorTask);

        comments = new ArrayList<Comment>();
        subPlannerTasks = new ArrayList<AbstractPlannerTask>();
        CreatorBasicTask creatorBasicTask = (CreatorBasicTask) abstractCreatorTask;
        for (AbstractCreatorTask subTask : creatorBasicTask.getSubTasks()) {
            if (subTask.isBasicTask()) {
                subPlannerTasks.add(new BasicPlannerTask(subTask));
            } else {
                subPlannerTasks.add(new SimpleMeetingPlannerTask(subTask));
            }
        }
    }

    public void reInitializeStartDate(Date startDate) {
        setStartDate(startDate);
        updateForSubTasks(subPlannerTasks, startDate);
    }

    private void updateForSubTasks(List<AbstractPlannerTask> abstractPlannerTasks, Date startDate) {

        Date newStartDate = startDate;
        List<Date> dates = new ArrayList<Date>();
        for (AbstractPlannerTask abstractPlannerTask : abstractPlannerTasks) {

            int id = abstractPlannerTask.getDependencyTaskId() - 1;
            if (id >= 0 && id < dates.size()) {
                newStartDate = dates.get(id);
                newStartDate = new Date(newStartDate.getTime() + abstractPlannerTask.getDependencyTaskTime());
            } else {
                if (dates.size() > 0) {
                    newStartDate = dates.get(dates.size() - 1);
                }
            }

            dates.add(newStartDate);
            TaskHelper.updateTime(abstractPlannerTask, abstractPlannerTasks, newStartDate);
        }
    }

    @XmlAttribute()
    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    @XmlElement()
    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    @XmlElement()
    public List<AbstractPlannerTask> getSubTasks() {
        return subPlannerTasks;
    }

    public void setSubTasks(List<AbstractPlannerTask> subPlannerTasks) {
        this.subPlannerTasks = subPlannerTasks;
    }

    @Override
    public boolean isFinished() {
        if (subPlannerTasks.size() != 0) {
            for (AbstractPlannerTask subPlannerTask : subPlannerTasks) {
                if (!subPlannerTask.isFinished()) {
                    return false;
                }
            }

            return true;
        }

        return progress == 100;
    }
}
